/*
 *  math.c
 *  Asset modeling
 *
 *  Created by Peeter Meos on 27.02.11.
 *  Copyright 2011 NC3A CAT1. All rights reserved.
 *
 */

#include "math.h"


double uniform_dist()
{
	// Simple rand() probably need to make it better later
	
	return (double)rand() / (double)RAND_MAX;
}

double exp_dist(double lambda)
{
	return -log(uniform_dist()) / lambda;
}

double deg2rad(double deg)
{
	return deg / 180.0 * PI;
}

double distance(double x1, double y1, double x2, double y2)
{
    const double R = 6371.0; // Earth's radius in km;
	
	double dLat = deg2rad(x2) - deg2rad(x1);
	double dLon = deg2rad(y2) - deg2rad(y1);
	
	double a = sin(dLat / 2.0) * sin(dLat / 2.0) +
			   cos(deg2rad(x1)) * cos(deg2rad(x2)) * 
			   sin(dLon / 2.0) * sin(dLon / 2.0); 
	
	double c = 2.0 * atan2(sqrt(a), sqrt(1.0 - a));
	
	return R * c;
	
/*
	double dLat;
	double dLon;
	double c;
	
	dLat = 111.13295 - 0.55982 * cos(2.0 * x1) + 0.00012 * cos(5.0 * x1);
	dLon = abs(y1 - y2);
	
	c = acos(sin(x1) * sin(x2) + cos(x1) * cos(x2) * cos(dLon)) * (180.0 / PI ) * dLat;
	
	return c;

 */
}